from django.shortcuts import render
from django.http import HttpResponse
from django.views import View
from rest_framework.views import APIView
from teacherSys.models import *
import jsonpickle


# Create your views here.


class StudentLogin(View):

    def get(self, request):

        return HttpResponse("学生登录页面")

    def post(self, request):

        userid = request.POST.get("userid", "")
        pwd = request.POST.get("pwd", "")
        user = Students.objects.filter(userid=userid, password=pwd)
        if user:
            request.session['user'] = jsonpickle.dumps(user[0])
            return HttpResponse("登录成功")
        else:
            return HttpResponse("用户名或密码错误")


class TeacherLogin(View):

    def get(self, request):

        return HttpResponse("教师登录页面")

    def post(self, request):

        userid = request.POST.get("userid", "")
        pwd = request.POST.get("pwd", "")
        user = Teacher.objects.filter(userid=userid, password=pwd)
        if user:
            request.session['user'] = jsonpickle.dumps(user[0])
            return HttpResponse("登录成功")
        else:
            return HttpResponse("用户名或密码错误")


class StudentsRegister(View):

    def get(self, request):

        return HttpResponse("学生注册界面")

    def post(self, request):
        identity = request.POST.get("identity", "0")
        name = request.POST.get("name", "")
        userid = request.POST.get("userid", "")
        password = request.POST.get("password", "")
        phone = request.POST.get("phone", "")
        if identity == "0":
            try:
                user = Students.objects.get(userid)
                if user:
                    return HttpResponse("用户已存在")
            except Students.DoesNotExist:
                user = Students.objects.create(name=name, userid=userid, password=password, phone=phone)
                request.session['user'] = jsonpickle.dumps(user)
            return HttpResponse("用户创建成功")
        else:
            try:
                user = Teacher.objects.get(userid)
                if user:
                    return HttpResponse("用户已存在")
            except Teacher.DoesNotExist:
                user = Teacher.objects.create(name=name, userid=userid, password=password)
                request.session['user'] = jsonpickle.dumps(user)
            return HttpResponse("用户创建成功")


class TeacherRegister(View):

    def get(self, request):

        return HttpResponse("教师注册界面")

    def post(self, request):

        pass


class StudentIndex(View):

    def get(self, request):
        pass

    def post(self, request):
        classes = request.POST.get("class")


class TeacherIndex(View):

    def get(self, request):
        pass

